package com.edais.service.imp;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.edais.service.CenterService;
import com.edais.service.CreditAssignService;
import com.edais.util.Arrays;
import com.edais.util.CommonUtil;
import com.edais.util.Consts;
import com.edais.util.JsonUtil;
import com.edais.util.Message;

@Service

public class CreditAssignServiceImp implements CreditAssignService {
	private static final Log logger = LogFactory.getLog(CreditAssignServiceImp.class);
	
	@Autowired
	private CenterService centerService;
	
	/**
	 * E330015债权转让
	 * 
	 * */
	@Override
	public String dealCreditAssign(String jsonStr) throws Exception {
		Map<String, Object> rtnMap = new HashMap<String, Object>();
		
		/**
		 * E330015债权转让
		 * */
		String rtnStr = CommonUtil.getCoreValue(jsonStr);
		JSONObject jsonObject = JSONObject.fromObject(rtnStr);
		
		if(Consts.SUCCESS_CODE.equals(jsonObject.get("rescode"))){
			rtnMap.put("totalNum", jsonObject.getString("totalNum"));            // 总记录数
			rtnMap.put("totalCreditValue", jsonObject.getString("totalCreditValue")); //当前债权总价值
			rtnMap.put("totalExitAmount", jsonObject.getString("totalExitAmount"));  // 可退出签约中总金额
			
			// 设置返回code
			return CommonUtil.setResultStringCn(
				rtnMap,Consts.SUCCESS_CODE, Consts.SUCCESS_DESCRIBE, "");
		} else {
			logger.debug("=====核心接口调用异常=====");
			logger.debug("rescode：" + jsonObject.getString("rescode") + "；  resmsg：" + jsonObject.getString("resmsg"));
			return CommonUtil.setResultStringCn(
				new HashMap<String, Object>(),Consts.ERROR_CODE, Consts.ErrorMsg.MSG00001,Message.CnDescriptionExpression.BUSINESSE_ERROR);
		}
	}

	/**
	 * E330017获取某订单下可转让债权列表
	 * 
	 * */
	@Override
	public JSONObject queryOrderAssignCredit(Map<String, Object> map) throws Exception {
		if(map == null || map.size() == 0){
			return null;
		}
		String userId = (null == map.get("userId").toString() ? "" : map.get("userId").toString());
		String orderId = (null == map.get("orderId").toString() ? "" : map.get("orderId").toString());
		
		String transCode = "E330017";

		// 设置报文体报文数据
		Map<String, Object> pageModel = new HashMap<String, Object>();
		pageModel.put("userId", userId);
		pageModel.put("orderId", orderId);

		String jsonStr = CommonUtil.constructJsonStr(transCode, userId, pageModel);// 构建json字符串
		String rtnStr = CommonUtil.getCoreValue(jsonStr);
		JSONObject jsonObject = JSONObject.fromObject(rtnStr);
		
		return jsonObject;
	}

	/**
	 * E330015债权转让
	 * 
	 * */
	@Override
	public JSONObject doCreditAssign(Map<String, Object> map) throws Exception {
		if(map == null || map.size() == 0){
			return null;
		}
		// 获取用户的基本信息
		String userId = (null == map.get("userId").toString() ? "" : map.get("userId").toString());
		String creditAcctCIDs = (null == map.get("creditAcctCIDs").toString() ? "" : map.get("creditAcctCIDs").toString());
		String signFlag = (null == map.get("signFlag").toString() ? "" : map.get("signFlag").toString());
		String orderId = (null == map.get("orderId").toString() ? "" : map.get("orderId").toString());
		
		String transCode = "E330015";

		// 设置报文体报文数据
		Map<String, Object> pageModel = new HashMap<String, Object>();
		pageModel.put("userId", userId);
		pageModel.put("creditAcctCIDs", creditAcctCIDs);
		pageModel.put("signFlag", signFlag);
		pageModel.put("orderId", orderId);

		String jsonStr = CommonUtil.constructJsonStr(transCode, userId, pageModel);// 构建json字符串
		String rtnStr = CommonUtil.getCoreValue(jsonStr);
		JSONObject jsonObject = JSONObject.fromObject(rtnStr);
		
		return jsonObject;
	}

	/**
	 * 一键转让
	 */
	@Override
	@Transactional
	public Map<String, Object> doQuickCreditAssign(Map<String, Object> map) throws Exception {
		Map<String, Object> rtnMap = new HashMap<String, Object>();
		String[] ids = null;
		// 获取某订单下可转让债权列表(E330017)
		JSONObject jsonObject = queryOrderAssignCredit(map);
		if(Consts.SUCCESS_CODE.equals(jsonObject.getString("rescode"))){
			JSONArray jsonArr = jsonObject.getJSONArray("list");
			if(jsonArr != null && jsonArr.size() > 0){
				List<Map<String, Object>> list = JsonUtil.toList(jsonArr);
				ids = new String[list.size()];
				for (int i = 0; i < list.size(); i++) {
					Map<String, Object> tmpMap = list.get(i);
					ids[i] = tmpMap.get("creditId").toString();
				}
				map.put("creditAcctCIDs", Arrays.toString(ids));
			} else {
				rtnMap.put("totalNum", 0);            // 总记录数
				rtnMap.put("totalCreditValue", 0.00); //当前债权总价值
//				rtnMap.put("totalExitAmount", 0.00);  // 可退出签约中总金额
//				return rtnMap;
				map.put("creditAcctCIDs", "");
			}
		}else{
			logger.error("E330017获取某订单下可转让债权列表异常" + jsonObject.getString("resmsg"));
			rtnMap.put("rescode", Consts.ERROR_CODE);
			rtnMap.put("resmsg", Consts.ErrorMsg.MSG00001);
			rtnMap.put("resmsg_cn", Message.CnDescriptionExpression.BUSINESSE_ERROR);
			return rtnMap;
		}
		
		//E330015债权转让
		
		JSONObject jsonObject1 = doCreditAssign(map);
		if(Consts.SUCCESS_CODE.equals(jsonObject1.get("rescode"))){
			rtnMap.put("totalNum", jsonObject1.getString("totalNum"));            // 总记录数
			rtnMap.put("totalCreditValue", jsonObject1.getString("totalCreditValue")); //当前债权总价值
			rtnMap.put("totalExitAmount", jsonObject1.getString("totalExitAmount"));  // 可退出签约中总金额
		} else {
			logger.debug("=====核心接口调用异常=====");
			logger.debug("rescode：" + jsonObject.getString("rescode") + "；  resmsg：" + jsonObject.getString("resmsg"));
			rtnMap.put("rescode", Consts.ERROR_CODE);
			rtnMap.put("resmsg", Consts.ErrorMsg.MSG00001);
			rtnMap.put("resmsg_cn", Message.CnDescriptionExpression.BUSINESSE_ERROR);
		}
		return rtnMap;
	}
}
